<?php
/**
 * 节点模型
 *
 * @Author: yuanji <fenglangyj@139.com>
 * @DateTime: 12-11-3 下午1:44
 * @Copyright (c) 2012 http://www.magentoui.com All rights reserved.
 * @Version  $Id: sss.php,v 1.0 uw Exp $
 * @Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 */
class NodeModel extends CommonModel {
	protected $_validate	=	array(
		array('name','checkNode','节点已经存在',0,'callback'),
		);

	public function checkNode() {
		$map['name']	 =	 $_POST['name'];
		$map['pid']	=	isset($_POST['pid'])?$_POST['pid']:0;
        $map['status'] = 1;
        if(!empty($_POST['id'])){
			$map['id']	=	array('neq',$_POST['id']);
        }
		$result	=	$this->where($map)->field('id')->find();
        if($result) {
        	return false;
        }else{
			return true;
		}
	}
    /**
     * 获取登录用户的权限树
     * @return array|bool
     */
    public function getNodeTree(){
        if(isset($_SESSION[C('USER_AUTH_KEY')])) {
            $menu  = array();
            if(isset($_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]]) and false){
                //如果已经缓存，直接读取缓存
                $menu   =   $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]];
            }else {
                $list = $this->field('name,title,group_id as parent_id')
                    ->where(array('level'=>2,'status'=>1,'pid'=>1))
                    ->order('sort DESC')
                    ->select();
                $Group = D('Group');
                $group_list = $Group->where(array('status'=>1))->order('parent_id,sort DESC')->getField('id,name,title,parent_id');

                $accessList = $_SESSION ['_ACCESS_LIST'];
                //筛选出有权限的节点
                $node = array();
                foreach ( $list as $key => $module ) {
                    if(isset ($accessList[strtoupper (APP_NAME)][strtoupper($module ['name'])]) || $_SESSION['administrator']) {//如果具备访问权限
                        //设置模块访问权限
                        $module ['access'] = 1;
                        if(!empty($group_list[$module['parent_id']])){
                            $node[$module['parent_id']][] = $module;
                        }
                    }
                }
                $group_tree = array();
                foreach($group_list as $group){
                    if(isset($group_list[$group['parent_id']])){
                        $group_list[$group['parent_id']]['node'][] = &$group_list[$group['id']];
                    }else{
                        $group_tree[]= &$group_list[$group['id']];
                    }
                }
                $menu=$this->getMenu($group_tree,$node);
                //缓存菜单访问
                $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]]	=	$menu;
            }
            return $menu;
        }
        return false;
    }

    /**
     * 迭代菜单项
     * @param $group_menu 分组树
     * @param $node 有权限的节点
     * @return array 返回树
     */
    private function getMenu($group_menu,$node){
        $menu = array();
        foreach($group_menu as $group){
            if(!empty($node[$group['id']])){
                $group['son'] = $node[$group['id']];
            }
            if(!empty($group['node'])){
                $tree = $this->getMenu($group['node'],$node);
                if(!empty($tree)){
                    if(empty($group['son'])){
                        $group['son'] = $tree;
                    }else{
                        $group['son'] = array_merge_recursive($tree,$group['son']);
                    }
                }
            }
            if(!empty($group['son'])){
                $menu[] = $group;
            }
        }
        return $menu;
    }
}
?>